﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.Drawing;
using System.Collections;

namespace WiMo.Games.Premium.GDIDisplay.ImageAPI
{
    static partial class GraphicsExtension
    {
        public static Bitmap GetGradientRectangle(WiMo.Games.Rectangle rc, Color startColorValue, Color endColorValue, FillDirection fillDirection)
        {
            Bitmap outputImage = new Bitmap(rc.Width, rc.Height);
            // Create temporary graphics
            using (var gx = Graphics.FromImage(outputImage))
                GradientFill.Fill(gx, rc.ToDrawingRect(), startColorValue.ToDrawingColor(), endColorValue.ToDrawingColor(), fillDirection);

            return outputImage;
        }

        public static void FillGradientRectangle(this Graphics gx, System.Drawing.Rectangle rc, System.Drawing.Color startColorValue, System.Drawing.Color endColorValue, FillDirection fillDirection)
        {
            GradientFill.Fill(gx, rc, startColorValue, endColorValue, fillDirection);
        }

        public static void DrawGradientRoundedRectangle(this Graphics gx, WiMo.Games.Rectangle rc, Color startColorValue, Color endColorValue, Color borderColor, System.Drawing.Size size)
        {
            var bitmap = GetGradiendRoundedRectangle(new WiMo.Games.Rectangle(0, 0, rc.Width, rc.Height), startColorValue, endColorValue, borderColor, size);
            gx.DrawImage(bitmap, rc);
        }

        public static Bitmap GetGradiendRoundedRectangle(WiMo.Games.Rectangle rc, Color startColorValue, Color endColorValue, Color borderColor, System.Drawing.Size radius)
        {
            var outputImage = new Bitmap(rc.Width, rc.Height);

            using (var gx = Graphics.FromImage(outputImage))
            {

                GradientFill.Fill(gx, rc.ToDrawingRect(), startColorValue.ToDrawingColor(), endColorValue.ToDrawingColor(), FillDirection.TopToBottom);

                var roundedRect = rc;
                roundedRect.Width--;
                roundedRect.Height--;

                var maskColor = Color.Yellow;

                using ( var mask = GetMask(roundedRect, borderColor, radius, maskColor))
                    gx.DrawImage(mask, new Location(0, 0), maskColor);
            }

            return outputImage;
        }

    }
}
